<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="header-title">核销管理</div>
      </div>
     
      <el-form :inline="true" :model="tableFrom" class="demo-form-inline">
        <el-form-item>
          <el-button type="success" @click="handleAdd">新增</el-button>
          <el-button type="danger" @click="handleBatchDelete" :disabled="multipleSelection.length === 0">批量删除</el-button>
        </el-form-item>
        <el-form-item label="会员搜索：">
          <el-input
            v-model="tableFrom.nickname"
            placeholder="请输入会员昵称"
            class="selWidth"
            clearable
            @keyup.enter.native="getList"
          ></el-input>
        </el-form-item>
        <el-form-item label="项目搜索：">
          <el-input
            v-model="tableFrom.activity_name"
            placeholder="请输入核销项目"
            class="selWidth"
            clearable
            @keyup.enter.native="getList"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" min-width="60"></el-table-column>
        <el-table-column prop="nickname" label="会员昵称" min-width="100"></el-table-column>
        <el-table-column prop="activity_name" label="核销项目" min-width="120"></el-table-column>
        <el-table-column prop="add_time" label="添加时间" min-width="150"></el-table-column>
        <el-table-column prop="update_time" label="更新时间" min-width="150"></el-table-column>
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  verificationListApi,
  verificationDeleteApi,
  verificationCreateApi,
  verificationUpdateApi
} from '@/api/activity'
import { roterPre } from '@/settings'
export default {
  name: 'Verification',
  data() {
    return {
      roterPre: roterPre,
      tableFrom: {
        page: 1,
        limit: 20,
        nickname: '',
        activity_name: ''  // 修改为activity_name，与后端接口对应
      },
      tableData: {
        data: [],
        total: 0
      },
      listLoading: true,
      multipleSelection: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.listLoading = true
      verificationListApi(this.tableFrom).then(res => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    // 添加
    handleAdd() {
      this.$modalForm(verificationCreateApi()).then(() => this.getList())
    },
    // 编辑
    handleEdit(row) {
      this.$modalForm(verificationUpdateApi(row.id)).then(() => this.getList())
    },
    // 删除
    handleDelete(id) {
      this.$modalSure('确定要删除该核销记录吗？').then(() => {
        verificationDeleteApi(id).then(({ message }) => {
          this.$message.success(message)
          this.getList()
        })
      })
    },
    // 批量删除
    handleBatchDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录')
        return
      }
      
      const ids = this.multipleSelection.map(item => item.id)
      this.$modalSure(`确定要删除选中的${ids.length}条记录吗？`).then(() => {
        // 这里需要后端提供批量删除接口，如果没有，可以循环调用单个删除
        const deletePromises = ids.map(id => verificationDeleteApi(id))
        
        Promise.all(deletePromises).then(() => {
          this.$message.success('批量删除成功')
          this.getList()
        }).catch(error => {
          this.$message.error('批量删除失败：' + error.message)
        })
      })
    },
    // 表格选择项变化
    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },
    // 分页
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    // 每页数量
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    // 重置
    resetForm() {
      this.tableFrom = {
        page: 1,
        limit: 20,
        nickname: '',
        activity_name: ''
      }
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.selWidth {
  width: 220px;
}
</style>